<?php

namespace addons\member\model;

use think\Db;
/**
 * 用户资产
 *
 * @author shilinqing
 */
class Balance extends \web\common\model\BaseModel {

    protected function _initialize() {
        $this->tableName = 'member_balance';
    }

    /**
     * get user balance by type
     * @param type $user_id
     * @param type $type
     * @return int
     */
    public function getBalanceByType($user_id, $type) {
        $where['user_id'] = $user_id;
        $where['b.type'] = $type;
        $data = $this->field('b.*,c.name')->alias('b')->join('sys_balance_conf c','c.id=b.type')->where($where)->find();
        return $data;
    }

    /**
     * 根据类型获取余额字段
     * @param type $user_id
     * @param type $type
     * @return int
     */
    public function getBalanceAmountByType($user_id, $type) {
        $where['user_id'] = $user_id;
        $where['type'] = $type;
        $data = $this->where($where)->value('amount');
        if($data === null){
            $where['update_time'] = NOW_DATETIME;
            $id = $this->add($where);
            if($id > 0) $data = 0;
        }
        return $data;
    }

    public function getUserBalanceList($user_id) {
        $c = new \addons\config\model\BalanceConf();
        $sql = 'select id,amount,type,address from ' . $this->getTableName() . ' where type IN (1,2,3,4) AND user_id=' . $user_id;
        $sql = 'select a.id,a.amount,a.address,b.name,b.type from(' . $sql . ') a left join ' . $c->getTableName() . ' b on a.type=b.id where b.is_show = 1 order by a.type asc';
        return $this->query($sql);
    }


    public function getUserAddressList($user_id) {
        $c = new \addons\config\model\BalanceConf();
        $sql = 'select id,amount,address,type from ' . $this->getTableName() . ' where type IN (1,2,3,4) AND user_id=' . $user_id;
        $sql = 'select a.id,a.address,b.name,b.type from(' . $sql . ') a left join ' . $c->getTableName() . ' b on a.type=b.id';
        return $this->query($sql);
    }

//    $c = new \addons\config\model\BalanceConf();
    public function getList($pageIndex = -1, $pageSize = -1, $filter = '', $fields = '', $order = 'id desc') {
        $c = new \addons\config\model\BalanceConf();
        $sql = 'select tab.*,c.name from ' . $this->getTableName() . ' as tab left join ' . $c->getTableName() . ' c on tab.type=c.id';
        if (!empty($filter))
            $sql = 'select * from (' . $sql . ') t where ' . $filter;
        return $this->getDataListBySQL($sql, $pageIndex, $pageSize, $order);
    }

    /**
     * 更新用户资产
     * @param type $user_id
     * @param type $type
     * @param type $amount 变动金额
     * @param type $change 变动类型，false 减值，true增值
     * @return type
     */
    public function updateBalance($user_id, $type, $amount, $change = false) {
        $map = array();
        $map['user_id'] = $user_id;
        $map['type'] = $type;
        $data = $this->where($map)->find();
        if (!$data) {
            $data['user_id'] = $user_id;
            $data['before_amount'] = 0;
            $data['amount'] = 0;
            $data['type'] = $type;
            $id = $this->add($data);
            if (!$id) return false;
        }else{
            $id = $data['id'];
        }
        $data['update_time'] = NOW_DATETIME;
        $data['before_amount'] = $data['amount'];
        if ($change) {
            $data['amount'] = Db::raw('amount+'.$amount);
        } else {
            $data['amount'] = Db::raw('amount-'.$amount);
        }
        $res = $this->where('id',$id)->update($data);
        if (!$res) return false;
        if ($change) {
            $data['amount'] = bcadd($data['before_amount'], $amount,8);
        } else {
            $data['amount'] = bcsub($data['before_amount'], $amount,8);
            if($data['amount']<0) return false;
        }
        return $data;
    }
   

    /**
     * 验证手续费所需可用余额是否足够
     */
    public function verifyStock($user_id, $amount, $type = 1) {
        $where['user_id'] = $user_id;
        $where['type'] = $type;
        $where['amount'] = array('>=', $amount);
        return $this->where($where)->find();
    }

    /**
     * otc确认交易余额操作
     * @param type $order_id
     * @return boolean
     */
    public function otcTradingConfirm($order_id) {
        $m = new \addons\otc\model\OtcOrder();
        $recordM = new \addons\member\model\TradingRecord();
        $m->startTrans();
        $order = $m->getDetail($order_id);
        $order['status'] = 4; //完成
        unset($order['pay_detail_json']);
        $update_status = $m->save($order);
        if (empty($update_status)) {
            $m->rollback();
            return false;
        }
        $buy_user_id = $order['buy_user_id'];
        $user_id = $order['user_id'];
        $coin_id = $order['coin_id'];
        $type = $order['type'];
        $tax_amount = $order['tax_amount'];
        $amount = $order['amount'];
        $total_amount = $order['total_amount'];
        if ($type == 1) {
            //买单 buy_user_id 扣除余额 , user_id 添加余额 ,手续费从total_amount 扣除
            $out_asset = $this->updateOtcAsset($buy_user_id, $amount, $coin_id);
            if (empty($out_asset)) {
                $m->rollback();
                return false;
            }
            $out_before_amount = $out_asset['amount'] + $amount;
            $out_record_id = $recordM->addRecord($buy_user_id, $coin_id, $amount, $out_before_amount, $out_asset['amount'], 1, 0, $user_id, '', '', '用户交易扣除');
            if (empty($out_record_id)) {
                $m->rollback();
                return false;
            }
            $_amount = $amount - $tax_amount;
            $in_asset = $this->updateBalance($user_id, $_amount, $coin_id, 1);
            if (empty($in_asset)) {
                $m->rollback();
                return false;
            }
            $in_record_id = $recordM->addRecord($user_id, $coin_id, $_amount, $in_asset['before_amount'], $in_asset['amount'], 1, 1, $buy_user_id, '', '', '用户交易增加');
            if (empty($in_record_id)) {
                $m->rollback();
                return false;
            }
            $m->commit();
            return true;
        } else {
            //卖单 user_id 扣除total_amount余额, buy_user_id 添加余额 amount
            $out_asset = $this->updateOtcAsset($user_id, $total_amount, $coin_id);
            if (empty($out_asset)) {
                $m->rollback();
                return false;
            }
            $out_before_amount = $out_asset['amount'] + $total_amount;
            $out_record_id = $recordM->addRecord($user_id, $coin_id, $total_amount, $out_before_amount, $out_asset['amount'], 1, 0, $buy_user_id, '', '', '用户交易扣除');
            if (empty($out_record_id)) {
                $m->rollback();
                return false;
            }
            $in_asset = $this->updateBalance($buy_user_id, $amount, $coin_id, 1);
            if (empty($in_asset)) {
                $m->rollback();
                return false;
            }
            $in_record_id = $recordM->addRecord($buy_user_id, $coin_id, $amount, $in_asset['before_amount'], $in_asset['amount'], 1, 1, $user_id, '', '', '用户交易增加');
            if (empty($in_record_id)) {
                $m->rollback();
                return false;
            }
            $m->commit();
            return true;
        }
    }

    /**
     * 更新otc余额
     * @param type $user_id     操作用户
     * @param type $amount     数量
     * @param type $type    0=减少 1=增加
     */
    public function updateOtcBalance($user_id, $amount, $type = 0) {
        $data = $this->getBalanceByUserID($user_id);
        if ($type == 0) {
            $data['otc_frozen_amount'] = $data['otc_frozen_amount'] - $amount;
            $data['update_time'] = NOW_DATETIME;
            $ret = $this->save($data);
        } else {
            $before_amount = $data['amount'];
            $data['before_amount'] = $before_amount;
            $data['amount'] = $before_amount + $amount;
            $data['otc_frozen_amount'] = $data['otc_frozen_amount'] - $amount;
            $data['update_time'] = NOW_DATETIME;
            $ret = $this->save($data);
        }
        if ($ret > 0)
            return $data;
        else
            return '';
    }

}
